package com.unity3d.sp_qqdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        EditText userNameEdit=findViewById(R.id.editTextTextPersonName);
        EditText userPwdEdit=findViewById(R.id.editTextTextPassword);

        Button loginBtn=findViewById(R.id.button);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName=userNameEdit.getText().toString();
                String userPwd=userPwdEdit.getText().toString();
                if(userName.equals("starry")&&userPwd.equals("lx0411")){
                    //todo 跳转到主页面+保存用户名+登录状态

                    //SharedPreferences是接口，并不是类
                    /*
                    参数一：存储数据的文件名
                     */
                    //获取SharedPreferences对象

                    SharedPreferences sp=getSharedPreferences("user",MODE_PRIVATE);
                    //获取Editor对象的引用
                    SharedPreferences.Editor editor=sp.edit();
                    //将获取过来的值放入文件
                    editor.putString("username",userName);
                    editor.putBoolean("isLogin",true);

                    //最后要提交数据到本地
                    editor.commit();

                    //登录成功，跳转到用户首页界面
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();

                }else{
                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}